<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller {
	
	function __construct(){
		parent::__construct();
        $this->load->model(array('home_model','search/search_model'));
        $this->load->library(array('pagination','data_solr'));
        $this->load->helper(array('text','string','form','url'));
        ini_set("time_execution", 3600);
	
	}

	function index(){
		# Add data into Solr - core number 1 : collection1 - Indexing progress has been activated at the first - tony activated it on Fri Nov 29th, 2012 -> 13,300 docs have been indexed
         
		# Add data into Solr - core number 3 : tony2 - Indexing progress has been activated at the first - tony activated it on Fri Nov 29th, 2012 -> 13,300 docs have been indexed
		$input		=	$this->search_model->get();

        if($input){
        	$info	=	array();
        	foreach ($input as $key => $value){
        		$info[$key]['id']					=	$value['id'];
        		$info[$key]['title']				=	$value['title'];
        		//$info[$key]['title_nosign']			=	nosign_title($value['title']);
        		$info[$key]['title_link']			=	$value['title_link'];
        		$info[$key]['description']			=	$value['description'];
        		//$info[$key]['description_nosign']	=	nosign_title($value['description']);
        		$info[$key]['text']					=	$value['text'];
        		//$info[$key]['text_nosign']			=	nosign_title($value['text']);
        		//$this->data_solr->add($value);
        	}
        	foreach($info as $key => $value){
        		$this->data_solr->add($value);
        	}
        } 
        
		
		# Add data into Solr - core number 2 : tony1
		/* $input = $this->home_model->get_question();
		if($input){
			foreach ($input as $key => $value){
				
				$this->data_solr->add($value);
			}
		} */
		#Get Category for Filtering query search.
		$data['cate']	=	$this->home_model->get_question_cate();
		
		$this->load->view('welcome_message', $data);
	}
	
	function search($keyword = NULL, $page = 1){
		$keyword1 = preg_replace('/[\s\?\%20]+/is','+',$keyword);
		$keywor = preg_replace('/[\:\?\&\<\>\#\'\"\+]+/is',' ',$keyword1);
		
		# Connect to Solr Server
		$solr = new Apache_Solr_Service( 'localhost', '8080', '/solr' );
		
		if ( ! $solr->ping() ) {
			echo 'Solr service not responding.';
			exit;
		}
		
		# Create documents
		$docs	=	$this->home_model->get();
		
		$documents = array();
		
		foreach ( $docs as $item => $fields ) {
		
			$part = new Apache_Solr_Document();
		
			foreach ( $fields as $key => $value ) {
				if ( is_array( $value ) ) {
					foreach ( $value as $data ) {
						$part->setMultiValue( $key, $data );
					}
				}
				else {
					$part->$key = $value;
				}
			}
		
			$documents[] = $part;
		}
			//
		  //
		  // Load the documents into the index
		  // 
		  try {
		    $solr->addDocuments( $documents );
		    $solr->commit();
		    $solr->optimize();
		  }
		  catch ( Exception $e ) {
		    echo $e->getMessage();
		  }
		  $limit = 20;
		  $offset = ($page - 1) * $limit;
		  
		  
		  	//$query = $_REQUEST['q'];
		  
		  	$queries = array();
		  	$queries[0] = $keywor;
		  	$additionalParameters = array(
		  			'sort'=> 'id asc',
		  			'hl' => 'true',
		  			'hl.fl' => 'text'
		  	);
		  	
		  	foreach ( $queries as $query ) {
		  		$response = $solr->search( $query, $offset, $limit, $additionalParameters );
		  		
		  		if ( $response->getHttpStatus() == 200 ) {
		  			// print_r( $response->getRawResponse() );
		  
		  			if ( $response->response->numFound > 0 ) {
		  				$total_num =  $response->response->numFound;
		  				foreach ($response->highlighting as $k => $v){
		  					$rows[$k] =	$v->text; 
		  				}	
		  			}
		  		}else{
		  			echo $response->getHttpStatusMessage();
		  		}
		  	}
		  			
		#pagin
		$url = base_url()."search/".$keyword;
		$total	=	ceil($total_num/$limit); 
  			$conf = array(
  					'total_rows'	=>	$total,
  					'cur_page'		=>	$page,
  					'base_url'		=>	$url,
  					'show_num'		=>	'yes',
  					'show_total' 	=>	'yes',
  					'show_first_last'	=>	'no',
  					'prev_class'=>	'pic',
  					'next_class'	=>	'pic',
  					'next_link'		=>	'Next >>',
  					'prev_link'		=>	'<< Prev'
  			);
  			$this->pagination->initialize($conf);
  			$data['pagnav'] = $this->pagination->non_ajax();
  			print_r($data['pagnav']);
		  			
		$data['info']	=	$rows;
		
		$this->load->view('hello', $data);
	}
	
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */